package com.snoob.serviceWorkOrder.workorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snoob.serviceWorkOrder.workorder.entity.WorkOrderEntity;
import com.snoob.serviceWorkOrder.workorder.mapper.WorkOrderMapper;
import com.snoob.serviceWorkOrder.workorder.service.WorkOrderService;
import com.snoob.springcloudalibaba.base.BaseException;
import com.snoob.springcloudalibaba.enums.SysStateEnum;
import com.snoob.springcloudalibaba.util.StrUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2022-05-14
 */
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrderEntity> implements WorkOrderService {
    /**
     * @描述 添加工单信息
     */
    @Override
    public String insertWorkOrder(WorkOrderEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        // 必填字段校验
        if (!StrUtil.isEffective(entity.getOrderName())) {
            throw new BaseException("工单名称缺失");
        }
        //
        entity.setOrderCode(UUID.randomUUID().toString()); // 工单编号
        entity.setOrderProgress(0);
        entity.setShowState(SysStateEnum.STATE_YES.getValue());
        entity.setCreateTime(new Date());
        boolean insertWorkOrderFlag = this.save(entity);
        return insertWorkOrderFlag ? entity.getOrderCode() : null;
    }
}
